﻿using SmartPlant.Model;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace SmartPlant.Services
{
    /// <summary>
    /// 对应Report控制器
    /// </summary>
    public class TemplateSettingService : BaseService, ITemplateSettingService
    {
        #region 基于report的func熟悉生成一个简单的报表
        public void GenerateExcelByReport(string proId, Dictionary<string, object> reportJob)
        {
            string uri = "";
            Report report = reportJob["report"] as Report;
            //report.GenerateFunc;
            uri = $"/api/Project/{proId}/Achievement-api/generate-report";

        }
        #endregion
        #region report相关
        /// <summary>
        /// 创建一个报表。
        /// ReportController/TemplateCreate
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<string>> CreateTemplateAsync(string proId, Report report)
        {
            string uri = $"/api/Project/{proId}/Report-Api";
            var rr = await PostBodyAsync<string, Report>(uri, report);//server端返回的是一个字符串
            return rr;
        }

        /// <summary>
        /// 删除一个。
        /// ReportController/Delete
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="templateType"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<string>> DeleteTemplateAsync(string proId, int templateType, Report report)
        {
            string url = $"/api/Project/{proId}/Report-Api/{report.ReportId}";
            var rr = await DeleteAsync<string>(url);
            return rr;

        }

        /// <summary>
        /// 更新模板（report本身，不是背后的文件）
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public async Task<Report> UpdateTemplateAsync(string proId, Report report)
        {
            string uri = $"/api/Project/{proId}/Report-Api";
            var r = await PutBodyAsync<Report, Report>(uri, report);
            return r.data;
        }

        /// <summary>
        /// 查询report表里的记录（所有的）
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="templateType"></param>
        /// <returns></returns>
        public async Task<List<Report>> GetTemplateLisAsync(string proId)
        {
            //string uri = $"/api/Project/{proId}/Achievement-api/templates/{templateType}";
            string uri = $"/api/Project/{proId}/Report-Api";
            var res = await GetAsync<List<Report>>(uri);
            return res.data;
        }
        /// <summary>
        /// 查询report表里的记录（按类型和输入输出）
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="type"></param>
        /// <param name="inOrOut"></param> 
        /// <returns></returns>
        public async Task<List<Report>> GetLisAsync(string proId, TemplateType type, int inOrOut)
        {
            string uri = $"/api/Project/{proId}/Report-Api/list/{type}/{inOrOut}";
            var res = await GetAsync<List<Report>>(uri);
            return res.data;
        }
        /// <summary>
        /// 查询report里的XML文件列表
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public async Task<List<string>> XMLListAsync(string proId, int inOrOut, TemplateType type, string templatePath)
        {
            string uri = $"/api/Project/{proId}/Report-Api/XML-List/{inOrOut}/{type}/{templatePath}";
            var res = await GetAsync<List<string>>(uri);
            return res.data;
        }
        ///// <summary>
        ///// 查询导入的模板（report记录）
        ///// </summary>
        ///// <param name="proId"></param>
        ///// <param name="templatetype"></param>
        ///// <returns></returns>
        //public async Task<List<Report>> GetTemplateLisAsync(string proId, string templatetype)
        //{
        //    string url = $"/api/Project/{proId}/Import/templates/{templatetype}";
        //    var data = await GetAsync<List<Report>>(url);
        //    return data;
        //}
        #endregion

        #region report背后，vault里的文件相关

        /// <summary>
        /// 包括输入和输出
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<Stream>> DownloadTemplateFileAsync(string proId, Report report)
        {
            string uri = "";
            if (report.Type == TemplateType.SPEC)
            {
                report.InOrOut = -1;
            }
            report.TemplatePath = GlobalObject.CurrentProject.ProjName;
            uri = $"/api/Project/{proId}/Report-Api/template-download";


            var data = await PostBodyStreamAsync(uri, report);
            return data;
        }



        public async Task<bool> UploadTemplateFileAsync(string proId, TemplateType templateType, Stream stream, string fileName, int InOrOut)
        {
            var templatepath = GlobalObject.CurrentProject.ProjName;
            string uri = $"/api/Project/{proId}/Report-Api/template-upload/{InOrOut}/{templateType}/{templatepath}";
            //if (templateType == 0 && InOrOut == 0)
            //{
            //    fileName = Path.GetFileName(((FileStream)stream).Name);
            //}
            return await PostFileAsync(uri, stream, fileName);

        }
        #endregion
    }
}
